package com.bbms.controller.reader;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bbms.entity.ErrorCode;
import com.bbms.entity.Page;
import com.bbms.entity.ReaderCtaegory;
import com.bbms.entity.Result;
import com.bbms.entity.SuccessCode;
import com.bbms.service.ReaderCategoryService;

/**
 * 读者类别控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/bbms/readerManage/readerCategory")
public class ReaderCategoryController {

	@Resource(name="ReaderCategoryServiceImpl")
	private ReaderCategoryService rcs;
	
	/**
	 * 获取分页数据
	 * @param page 当前页面
	 * @param rows 行数
	 * @return 分页数据
	 */
	@ResponseBody
	@RequestMapping(value="list",method=RequestMethod.POST)
	public Page getPage(int page,int rows) {
		Page data=rcs.getPage(page, rows);
		return data;
	}
	
	/**
	 * 获取所有数据（无分页）
	 * @return 所有数据
	 */
	@ResponseBody
	@RequestMapping(value="combobox",method=RequestMethod.POST)
	public Object getAllData() {
		Object object=JSON.toJSON(rcs.getAllData());
		return object;
	}
	
	/**
	 * 添加读者类别
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="insert",method=RequestMethod.POST)
	public Result insert(@ModelAttribute(name="rc")ReaderCtaegory rc){
		
		Result result=new Result();
		try{
			rcs.add(rc);
			result.setStatus(SuccessCode.insertSuccess.getCode());
			result.setMsg(SuccessCode.insertSuccess.getName());
		}catch(Exception e){
			e.printStackTrace();
			result.setStatus(ErrorCode.insertFailed.getCode());
			result.setMsg(ErrorCode.insertFailed.getName());
		}
		return result;
	}
	
	
	/**
	 * 修改读者类别信息
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="modify",method=RequestMethod.POST)
	public Result update(@ModelAttribute(name="rc")ReaderCtaegory rc){
		Result result=new Result();
		try{
			rcs.update(rc);
			result.setStatus(SuccessCode.updateSuccess.getCode());
			result.setMsg(SuccessCode.updateSuccess.getName());
		}catch(Exception e){
			e.printStackTrace();
			result.setStatus(ErrorCode.updateFailed.getCode());
			result.setMsg(ErrorCode.updateFailed.getName());
		}
		return result;
	}
	
	/**
	 * 删除读者类别信息
	 * @param rc 读者类别对象
	 * @return 反馈信息
	 */
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public Result delete(@ModelAttribute(name="rc")ReaderCtaegory rc){
		Result result=new Result();
		try{
			rcs.delete(rc);
			result.setStatus(SuccessCode.deleteSuccess.getCode());
			result.setMsg(SuccessCode.deleteSuccess.getName());
		}catch(Exception e){
			e.printStackTrace();
			result.setStatus(ErrorCode.deleteFailed.getCode());
			result.setMsg(ErrorCode.deleteFailed.getName());
		}
		return result;
	}
}
